package com.itheima.mp.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.itheima.mp.domain.enums.UserStatus;
import com.itheima.mp.domain.po.Address;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.vo.AddressVO;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.mapper.UserMapper;
import com.itheima.mp.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//ai调用的Copilot与 GPT , 会占内存  knife4j 就是对swagger的一个封装
@Service
//                                             指定底层使用的是哪个持久层
//                                               操作的实体类
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void deductBalanceById(Long id, int amount) {
        //1、判断用户是否存在
        User user = this.getById(id);
        //卫语句
        if (user == null || user.getStatus() == UserStatus.FREEZE) {
            throw new RuntimeException("用户不存");
        }
        //2、判断用户余额是否充足 当前用户的余额要大于等于要扣除的余额
        if (user.getBalance() < amount) {
            throw new RuntimeException("用户余额不足");
        }
        //3、扣减
        int remainBalance = user.getBalance() - amount;
        //如果扣减后余额为0, 则将用户status修改成冻结状态
        this.lambdaUpdate()
                .set(User::getBalance, remainBalance) //更改余额
                .set(remainBalance == 0, User::getStatus, UserStatus.NORMAL)
                .eq(User::getId, id)
                .eq(User::getBalance, user.getBalance()) //防止并发问题 乐观锁
                .update();
//        userMapper.deductBalanceById(amount,id);

    }

    @Override
    public UserVO queryUserAndAddressById(Long id) {
        //1、查询用户信息
        User user = userMapper.selectById(id);
        //需要的是VO转换成VO
        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
        //2、查询用户地址信息
        List<Address> list = Db.lambdaQuery(Address.class)
                .eq(Address::getUserId, id)
                .list();
        //需要的是VO转换成VO
        List<AddressVO> addressVOS = BeanUtil.copyToList(list, AddressVO.class);
        //封装进userVO中
        userVO.setAddresses(addressVOS);
        //3、封装数据并返回
        return userVO;
    }


    // 底层还是使用的是Mapper,
    @Override
    public List<UserVO> queryUserAndAddressByIds(List<Long> ids) {
        //1. 根据id集合查询用户列表
        List<User> list = this.listByIds(ids);
        List<UserVO> userVOList = BeanUtil.copyToList(list, UserVO.class);
        //2.根据id集合查询用户对应的地址列表 --> 如果一个用户有多个地址, 会有两条记录在下面的集合中
        List<Address> addressList = Db.lambdaQuery(Address.class).
                in(Address::getUserId, ids)
                .list();
        List<AddressVO> addressVOS = BeanUtil.copyToList(addressList, AddressVO.class);
        //希望可以将上面的列表转换为: map<用户id, 用户地址>
        // 这种写法后面会大量使用 -- > 按照id分组, 组成一个map, 键就是id
        Map<Long, List<AddressVO>> userAddressMap = addressVOS.stream().collect(Collectors.groupingBy(AddressVO::getUserId));

        for (UserVO userVO : userVOList) {
            List<AddressVO> addressVO = userAddressMap.get(userVO.getId());
            userVO.setAddresses(addressVO);
        }
        return userVOList;
    }

}
